package com.example.artwindow.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.artwindow.constants.HttpCodeEnum;
import com.example.artwindow.domain.dto.CommentPageDTO;
import com.example.artwindow.domain.po.Comment;
import com.example.artwindow.domain.dto.CommentAddDTO;
import com.example.artwindow.domain.po.User;
import com.example.artwindow.domain.vo.CommentUserVO;
import com.example.artwindow.domain.vo.CommentVO;
import com.example.artwindow.exception.UnauthorizedException;
import com.example.artwindow.result.ListResult;
import com.example.artwindow.result.Result;
import com.example.artwindow.service.CommentService;
import com.example.artwindow.mapper.CommentInfoMapper;
import com.example.artwindow.service.MovieService;
import com.example.artwindow.service.UserService;
import com.example.artwindow.tool.CopyProperties;
import com.example.artwindow.tool.TheadLocalTool;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author EiEi
* @description 针对表【comment_info】的数据库操作Service实现
* @createDate 2024-01-27 10:18:43
*/
@Service
@RequiredArgsConstructor
public class CommentServiceImpl extends ServiceImpl<CommentInfoMapper, Comment>
    implements CommentService {

	private final MovieService movieService;

	private final UserService userService;
	@Override
	public Result<Null> commentAdd(CommentAddDTO commentAddDTO) {
		Comment comment = BeanUtil.copyProperties(commentAddDTO, Comment.class);
		if (ObjectUtil.isNull(TheadLocalTool.getCurrentId())){
			throw new UnauthorizedException(HttpCodeEnum.ERR_NOT_KNOW);
		}
		movieService.countComment(commentAddDTO.getMovieId(),commentAddDTO.getCommentScore());
		save(comment);
		return Result.success();
	}

	@Override
	public Result<ListResult<CommentVO>> commentPage(CommentPageDTO commentPageDTO) {
		//查询评论
		LambdaQueryWrapper<Comment> commentLambdaQueryWrapper = new LambdaQueryWrapper<>();
		if (!ObjectUtil.isNull(commentPageDTO.getMovieId())){
			commentLambdaQueryWrapper.eq(Comment::getMovieId,commentPageDTO.getMovieId());
			commentLambdaQueryWrapper.isNull(Comment::getCommentRoot);
		}
		else
			commentLambdaQueryWrapper.eq(Comment::getCommentRoot,commentPageDTO.getCommentRoot());
		commentLambdaQueryWrapper.select();
		commentLambdaQueryWrapper.orderByDesc(Comment::getCreateTime);
		//分页
		Page<Comment> page = new Page<>(commentPageDTO.getPage(), commentPageDTO.getPageSize());
		page(page,commentLambdaQueryWrapper);
		//封装
		List<CommentVO> commentVOS = CopyProperties.copyProperties(page.getRecords(), CommentVO.class);
		Map<Long, Comment> commentMap = page.getRecords().stream().collect(Collectors.toMap(Comment::getId, t -> t));
		commentVOS.forEach(h -> {
			h.setCommentTime(new SimpleDateFormat("yyyy-MM-dd-hh:mm:ss").format(new Date(commentMap.get(h.getId()).getCreateTime())));
		});
		//查询用户信息
		List<Long> userIds = CollUtil.getFieldValues(commentVOS, "createUser", Long.class);
		List<CommentUserVO> list=new ArrayList<>();
		if(!userIds.isEmpty()){
			list = userService.selectCommentUsers(userIds);
		}
		//封装返回值
		//将用户信息插入
		ListResult<CommentVO> commentVOListResult = CopyProperties.setUser(commentVOS, list, page.getTotal(),ObjectUtil.isNull(commentPageDTO.getMovieId()));
		return Result.success(commentVOListResult);
	}
}




